import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:myapp/pages/knowledge/detail/knowledge_detail_tab_page.dart';
import 'package:myapp/pages/knowledge/knnowledge_vm.dart';
import 'package:myapp/route/route_utils.dart';
import 'package:provider/provider.dart';

import '../../repository/datas/knowledge_list_data.dart';

class KnowledgePage extends StatefulWidget {
  const KnowledgePage({super.key});

  @override
  State createState() {
    return _KnowledgePageState();
  }
}

class _KnowledgePageState extends State<KnowledgePage> {
  KnowledgeViewModel viewModel = KnowledgeViewModel();

  @override
  void initState() {
    super.initState();
    viewModel.knowledgeList();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) {
        return viewModel;
      },
      child: Consumer<KnowledgeViewModel>(
        builder: (context, vm, child) {
          return Scaffold(
            body: ListView.builder(
              itemCount: vm.list?.length ?? 0,
              itemBuilder: (context, index) {
                final item = viewModel.list?[index];
                return _itemView(item);
              },
            ),
          );
        },
      ),
    );
  }

  Widget _itemView(KnowledgeListData? item){
    return GestureDetector(
      onTap: (){
        //进入明细页面
        RouteUtils.push(context, KnowledgeDetailTabPage(tabList: item?.children,));
      },
      child: Container(
        padding: EdgeInsets.only(
          left: 15.w,
          right: 15.w,
          top: 10.h,
          bottom: 10.h,
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black), //边框颜色
          borderRadius: BorderRadius.circular(5.r), //边框圆角
        ),
        margin: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
        //外边框
        child: Row(
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    item?.name ?? "",
                    style: TextStyle(
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  SizedBox(height: 5.h),
                  Text(
                    viewModel.generalSubTitle(item?.children),
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: Colors.grey.shade600,
                    ),
                  ),
                ],
              ),
            ),
            Image.asset(
              "assets/images/img_arrow_right.png",
              width: 15.w,
              height: 20.h,
            ),
          ],
        ),
      ),
    );
  }

}

